<template>
  <div class="forgot-password-container">
    <div class="login-logo">WMS</div>
    
    <div class="return-login" @click="returnToLogin">
      <el-icon><Back /></el-icon>
      返回登录
    </div>
    
    <div class="forgot-password-content">
      <div class="forgot-password-form">
        <h2 class="form-title">忘记密码</h2>
        
        <!-- 步骤条 -->
        <div class="step-container">
          <div class="step-item" :class="{ 'active': currentStep >= 1, 'completed': currentStep > 1 }">
            <div class="step-number">
              <template v-if="currentStep <= 1">1</template>
              <el-icon v-else><Check /></el-icon>
            </div>
            <div class="step-text">账号验证</div>
          </div>
          <div class="step-line" :class="{ 'active': currentStep > 1 }"></div>
          <div class="step-item" :class="{ 'active': currentStep >= 2, 'completed': currentStep > 2 }">
            <div class="step-number">
              <template v-if="currentStep <= 2">2</template>
              <el-icon v-else><Check /></el-icon>
            </div>
            <div class="step-text">重置密码</div>
          </div>
          <div class="step-line" :class="{ 'active': currentStep > 2 }"></div>
          <div class="step-item" :class="{ 'active': currentStep >= 3 ,'completed': currentStep > 2}">
            <div class="step-number">
              <template v-if="currentStep < 3">3</template>
              <el-icon v-else><Check /></el-icon>
            </div>
            <div class="step-text">完成</div>
          </div>
        </div>
        
        <!-- 步骤1：账号验证 -->
        <div v-if="currentStep === 1" class="step-form">
          <el-form ref="verifyFormRef" :model="verifyForm" :rules="verifyRules">
            <el-form-item prop="phone">
              <el-input 
                v-model="verifyForm.phone" 
                placeholder="请输入手机号"
              />
            </el-form-item>
            
            <el-form-item prop="captcha">
              <div class="captcha-container">
                <el-input 
                  v-model="verifyForm.captcha" 
                  placeholder="请输入验证码" 
                />
                <el-button 
                  type="primary" 
                  :disabled="captchaDisabled" 
                  @click="sendCaptcha"
                >
                  {{ captchaText }}
                </el-button>
              </div>
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                class="form-button" 
                @click="verifyAccount"
              >
                下一步
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        
        <!-- 步骤2：重置密码 -->
        <div v-else-if="currentStep === 2" class="step-form">
          <el-form ref="resetFormRef" :model="resetForm" :rules="resetRules">
            <el-form-item prop="newPassword">
              <el-input 
                v-model="resetForm.newPassword" 
                type="password" 
                placeholder="请输入新密码"
                show-password
              />
            </el-form-item>
            
            <el-form-item prop="confirmPassword">
              <el-input 
                v-model="resetForm.confirmPassword" 
                type="password" 
                placeholder="请确认新密码"
                show-password
              />
            </el-form-item>
            
            <div class="password-tip">
              密码提示：需要包含数字、大写字母、小写字母、特殊字符其中三种组合, 且为8~16位密码
            </div>
            
            <el-form-item>
              <el-button 
                type="primary" 
                class="form-button" 
                @click="resetPassword"
              >
                下一步
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        
        <!-- 步骤3：完成 -->
        <div v-else-if="currentStep === 3" class="step-form success-form">
          <div class="success-icon">
            <el-icon><Check /></el-icon>
          </div>
          <h3>您的密码重置成功</h3>
          <el-button 
            type="primary" 
            class="form-button" 
            @click="returnToLogin"
          >
            重新登录
          </el-button>
        </div>
      </div>
    </div>
    
    <!-- <div class="login-footer">
      <span>如遇问题，联系客服热线</span>
	  <span class="login-footer-phone">	400-6529088</span>
    </div> -->
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Back, Check } from '@element-plus/icons-vue'

const router = useRouter()
const currentStep = ref(1)
const captchaDisabled = ref(false)
const captchaText = ref('发送验证码')

// 验证表单
const verifyFormRef = ref(null)
const verifyForm = reactive({
  phone: '',
  captcha: ''
})

// 重置密码表单
const resetFormRef = ref(null)
const resetForm = reactive({
  newPassword: '',
  confirmPassword: ''
})

// 表单验证规则
const verifyRules = reactive({
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { pattern: /^\d{6}$/, message: '验证码为6位数字', trigger: 'blur' }
  ]
})

const resetRules = reactive({
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' },
    { 
      pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,}$/, 
      message: '密码必须包含大小写字母、数字和特殊字符', 
      trigger: 'blur' 
    }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value !== resetForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      }, 
      trigger: 'blur' 
    }
  ]
})

// 发送验证码
const sendCaptcha = () => {
  if (!verifyForm.phone) {
    ElMessage.warning('请先输入手机号')
    return
  }
  
  // 校验手机号格式
  if (!/^1[3-9]\d{9}$/.test(verifyForm.phone)) {
    ElMessage.warning('请输入正确的手机号')
    return
  }
  
  captchaDisabled.value = true
  captchaText.value = '发送中...'
  
  // 模拟发送验证码请求
  setTimeout(() => {
    ElMessage.success(`验证码已发送至 ${verifyForm.phone}`)
    
    // 倒计时
    let count = 60
    captchaText.value = `${count}s`
    const timer = setInterval(() => {
      count--
      captchaText.value = `${count}s`
      if (count <= 0) {
        clearInterval(timer)
        captchaText.value = '发送验证码'
        captchaDisabled.value = false
      }
    }, 1000)
  }, 1500)
}

// 验证账号
const verifyAccount = () => {
  if (!verifyFormRef.value) return
  
  verifyFormRef.value.validate(valid => {
    if (!valid) return
    
    // 模拟验证过程
    setTimeout(() => {
      ElMessage.success('账号验证成功')
      currentStep.value = 2
    }, 800)
  })
}

// 重置密码
const resetPassword = () => {
  if (!resetFormRef.value) return
  
  resetFormRef.value.validate(valid => {
    if (!valid) return
    
    // 模拟重置密码过程
    setTimeout(() => {
      ElMessage.success('密码重置成功')
      currentStep.value = 3
    }, 800)
  })
}

// 返回登录页
const returnToLogin = () => {
  router.push('/login')
}
</script>

<style lang="scss" scoped>
.forgot-password-container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100vh;
  width: 100%;
  overflow: hidden;
  background-image: url('@/assets/images/background-img.png');
  background-size: cover;
  background-position: center;
}

.login-logo {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 32px;
  font-weight: bold;
  color: #0165FF;
}

.return-login {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  align-items: center;
  color: #121212;
  cursor: pointer;
  padding: 8px 16px;
  background-color: #fff;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 600;
  .el-icon {
    margin-right: 4px;
  }
}

.forgot-password-content {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  padding: 0 20px;
}

.forgot-password-form {
  width: 500px;
  background-color: #fff;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

.form-title {
  text-align: center;
  margin-bottom: 30px;
  font-size: 22px;
  color: #303133;
}

.step-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 40px;
}

.step-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  
  .step-number {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background-color: #DCDFE6;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    margin-bottom: 8px;
  }
  
  .step-text {
    font-size: 14px;
    color: #909399;
  }
  
  &.active {
    .step-number {
      background-color: #0165FF;
    }
    
    .step-text {
      color: #0165FF;
      font-weight: 500;
    }
  }
  
  &.completed {
    .step-number {
      background-color: #67C23A;
    }
  }
}

.step-line {
  width: 80px;
  height: 2px;
  background-color: #DCDFE6;
  margin: 0 10px;
  margin-bottom: 25px;
  
  &.active {
    background-color: #0165FF;
  }
}

.step-form {
  padding: 0 20px;
}

.captcha-container {
  display: flex;
  
  .el-input {
    flex: 1;
    margin-right: 10px;
  }
  
  .el-button {
    width: 120px;
  }
}

.form-button {
  width: 100%;
  margin-top: 20px;
  padding: 12px 0;
  font-size: 16px;
  background-color: #0165FF;
  border-color: #0165FF;
  
  &:hover, &:focus {
    background-color: #1975ff;
    border-color: #1975ff;
  }
}

.password-tip {
  margin-bottom: 10px;
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
}

.success-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30px 20px;
  
  .success-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background-color: #67C23A;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
    
    .el-icon {
      font-size: 40px;
    }
  }
  
  h3 {
    margin-bottom: 30px;
    font-size: 18px;
    color: #303133;
  }
}

.login-footer {
  text-align: center;
  padding: 20px 0;
  color: #606266;
  font-size: 16px;
  &-phone{
	  color: #0165FF;
	  font-weight:600;
  }
}
</style> 